import { addContractAPI, addEnterpriseAPI, deleteContractAPI, deleteEnterpriseAPI, fetchContractListAPI, fetchEnterpriseListAPI, fetchIndustryListAPI, findEnterpriseAPI, rentingQutAPI, updateEnterpriseAPI, uploadAPI, } from '@/api/park/enterpriseApi';

const actions = {
  /* 发送请求获取企业列表的action */
  async fetchEnterpriseList(context, pageParams) {
    const { data } = await fetchEnterpriseListAPI(pageParams);
    data.rows.forEach(item => {
      item.list = [];
    });
    context.commit('updateEnterpriseList', data);
  },

  /* 发送请求删除企业的action */
  async deleteEnterprise(context, id) {
    const { msg } = await deleteEnterpriseAPI(id);
    return msg;
  },

  /* 发送请求添加企业的action */
  async addEnterprise(context, formParams) {
    const { msg } = await addEnterpriseAPI(formParams);
    return msg;
  },

  /* 发送编辑添加企业的action */
  async updateEnterprise(context, formParams) {
    const { msg } = await updateEnterpriseAPI(formParams);
    return msg;
  },

  /* 用于发送请求添加合同的action */
  async addContract(context, formParams) {
    const { msg } = await addContractAPI(formParams);
    return msg;
  },
  /* 用于上传文件的action */
  async upload(context, formData) {
    const { data } = await uploadAPI(formData);
    return data;
  },

  /* 发送请求查找企业的action */
  async findEnterprise(context, id) {
    const { data } = await findEnterpriseAPI(id);
    return data;
  },

  /* 发送请求获取所在行业列表的action */
  async fetchIndustryList(context) {
    const { data } = await fetchIndustryListAPI();
    context.commit('updateIndustryList', data);
  },

  /* 发送请求获取企业租赁列表展开查看的action */
  async fetchContractList(context, id) {
    const { data } = await fetchContractListAPI(id);
    context.commit('updateContractList', { id, contractList: data });
  },
  /* 发送请求退租的action */
  async rentingQut(context, id) {
    const { msg } = await rentingQutAPI(id);
    return msg;
  },
  /* 发送请求删除合同 */
  async deleteContract(context, id) {
    const { msg } = await deleteContractAPI(id);
    return msg;
  },
};
const mutations = {
  /* 更新企业列表的mutation */
  updateEnterpriseList(state, enterpriseListInfo) {
    state.enterpriseListInfo = enterpriseListInfo;
  },
  /* 更新所在行业列表的mutation */
  updateIndustryList(state, industryList) {
    state.industryList = industryList;
  },
  /* 根据企业ID往当前企业的list数组中添加企业租赁列表 */
  updateContractList(state, { id, contractList }) {
    const currentEnterprise = state.enterpriseListInfo?.rows.find(item => item.id === id);
    currentEnterprise.list = contractList;
  },
};
const state = () => {
  return {
    enterpriseListInfo: {},
    industryList: [],
  };
};
const getters = {
  enterpriseList(state) {
    return state.enterpriseListInfo.rows;
  },
  total(state) {
    return state.enterpriseListInfo.total;
  },
};
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true,
};
